package org.edu.eece262.taskshare;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class TaskView extends LinearLayout {
	 
	private HashMap<View, ArrayList<View>> hmAssociation;
	private TaskShare tsCallback;

	public TaskView(Context context, TaskShare tsUser) {
		super(context);
		
		tsCallback = tsUser;
		
		this.setOrientation(VERTICAL);
		
		hmAssociation = new HashMap<View, ArrayList<View>>();
		
		LayoutInflater inflater = LayoutInflater.from(context);
		
		Cursor top = TaskDBAdapter.getTSDBAdapter(context).getTopLevelEntries();
		top.moveToFirst();
		
		addCursor(top, inflater, 0, TaskDBAdapter.getTSDBAdapter(context));
	
	}
	
	public void update(Context context) {
		this.removeAllViews();
		hmAssociation.clear();
		
		LayoutInflater inflater = LayoutInflater.from(context);
		
		Cursor top = TaskDBAdapter.getTSDBAdapter(context).getTopLevelEntries();
		top.moveToFirst();
		
		addCursor(top, inflater, 0, TaskDBAdapter.getTSDBAdapter(context));
	}
	
	public void updateSorted(Context context, String sortMethod) {
		this.removeAllViews();
		hmAssociation.clear();
		
		LayoutInflater inflater = LayoutInflater.from(context);
		
		Cursor top = TaskDBAdapter.getTSDBAdapter(context).getTopLevelEntriesSorted(sortMethod);
		top.moveToFirst();
		
		addCursor(top, inflater, 0, TaskDBAdapter.getTSDBAdapter(context));
	}
	
	private ArrayList<View> addCursor(Cursor top, LayoutInflater inflater, int indent, TaskDBAdapter tsdbAdapter) {
		ArrayList<View> rtn = new ArrayList<View>();
		
		top.moveToFirst();
		
		while(!top.isAfterLast()) {
			View v = inflater.inflate(R.layout.tasklistitem, this, false);
			this.addView(v);
			rtn.add(v);
			v.findViewById(R.id.llTaskItem).setPadding(indent, 0, 0, 0);
			
			TextView tvName = (TextView)v.findViewById(R.id.tvName);
			tvName.setText(top.getString(TaskDBAdapter.NAME_COLUMN));
			tvName.setOnClickListener(new ClickListener(top.getInt(0)));
			
			CheckBox cbDone = (CheckBox)v.findViewById(R.id.cbDone);
			if(top.getInt(TaskDBAdapter.DONE_COLUMN) == 1 || top.getInt(TaskDBAdapter.PERCENT_COMPLETE_COLUMN) == 100) {
				cbDone.setChecked(true);
			} else {
				cbDone.setChecked(false);
			}
			cbDone.setOnCheckedChangeListener(new CheckListener(top.getInt(0)));
			
			if(tsdbAdapter.getChildrenOf(top.getInt(0)).getCount() > 0) {				
				ImageView ivExpander = (ImageView)v.findViewById(R.id.ivExpander);
				hmAssociation.put(ivExpander, addCursor(tsdbAdapter.getChildrenOf(top.getInt(0)), inflater, indent + 20, tsdbAdapter));
				ivExpander.setVisibility(VISIBLE);
				ArrayList<View> alView = hmAssociation.get(ivExpander);
				for(int i = 0; i < alView.size(); i++) {
					alView.get(i).setVisibility(GONE);
				}
				ivExpander.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						ArrayList<View> alView = hmAssociation.get(v);
						if(alView.get(0).getVisibility() == GONE) {
							((ImageView) v).setImageResource(R.drawable.expander_ic_maximized);
							for(int i = 0; i < alView.size(); i++) {
								alView.get(i).setVisibility(VISIBLE);
							}
						} else {
							((ImageView) v).setImageResource(R.drawable.expander_ic_minimized);
							hideChildren(alView);
						}
						
					}
				});
				
			} else {
				v.findViewById(R.id.ivExpander).setVisibility(GONE);
			}
			
			top.moveToNext();
		}
		return rtn;
	}
	
	private class CheckListener implements OnCheckedChangeListener {
		
		int _id = 0;
		
		public CheckListener(int localIDtoMark) {
			_id = localIDtoMark;
		}
		
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if(isChecked) {
				TaskDBAdapter.getTSDBAdapter(null).markDone(_id, 1);
			} else {
				TaskDBAdapter.getTSDBAdapter(null).markDone(_id, 0);
			}
		}
	}
	
	private class ClickListener implements OnClickListener {
		private int iLocalID;
		
		ClickListener(int local_id) {
			super();
			iLocalID = local_id;
		}
		
		@Override
		public void onClick(View v) {
			tsCallback.Selected(iLocalID);
		}
	}
	
	private void hideChildren(ArrayList<View> avChildren) {
		for(int n = 0; n < avChildren.size(); n++) {
			View v = avChildren.get(n);
			v.setVisibility(GONE);
			ImageView ivExpander = (ImageView)v.findViewById(R.id.ivExpander);
			if(hmAssociation.containsKey(ivExpander)) {
				ivExpander.setImageResource(R.drawable.expander_ic_minimized);
				hideChildren(hmAssociation.get(ivExpander));
			}
		}
	}
}
